﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GFluentCode.Entity
{
    /// <summary>
    /// 数据库实体基类
    /// </summary>
    public class GFluentEntityBase
    {
        #region 静态方法

        /// <summary>
        /// 根级字符串
        /// </summary>
        public const string ROOT = "ROOT";

        /// <summary>
        /// 16位的GUID
        /// </summary>
        /// <returns></returns>
        public static string Guid16()
        {
            long i = 1;
            foreach (byte b in Guid.NewGuid().ToByteArray())
                i *= ((int)b + 1);
            return string.Format("{0:x}", i - DateTime.Now.Ticks);
        }

        /// <summary>
        /// 获取实体对应表名
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static string GetTableName<T>()
        {
            var desc = (GFluentTable[])typeof(T).GetCustomAttributes(typeof(GFluentTable), true);

            if (desc == null || desc.Length <= 0) return $"[{typeof(T).Name.Replace("Entity", "")}]";

            return $"[{desc[0].TableName}]";
        }

        /// <summary>
        /// 获取查询列
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static string GetColumns<T>()
        {
            var pros = typeof(T).GetProperties();

            var fields = string.Empty;

            foreach (var f in pros)
            {
                var attr = f.GetCustomAttributes(true);
                if (attr.Length <= 0)
                {
                    fields += $"[{f.Name}],";
                    continue;
                }

                var ca = attr[0] as GFluentColumn;
                if (ca.ColumnType.Equals(EnColumnType.Ignore)) continue;

                fields += $"{(string.IsNullOrWhiteSpace(ca.ColumnName) ? "["+ f.Name + "] As " : "["+ ca.ColumnName + "] As "+f.Name)},";
            }

            return fields.TrimEnd(',');
        }

        /// <summary>
        /// 获取主键
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static string GetPrimaryKey<T>()
        {
            var propertys = typeof(T).GetProperties();

            var primaryKey = string.Empty;

            GFluentColumn colAttr;

            foreach (var property in propertys)
            {
                var attr = property.GetCustomAttributes(true);

                if (attr.Length <= 0) continue;

                colAttr = attr[0] as GFluentColumn;
                if (!colAttr.ColumnType.Equals(EnColumnType.PrimaryKey)) continue;

                primaryKey = string.IsNullOrWhiteSpace(colAttr.ColumnName) ? property.Name : colAttr.ColumnName;
                break;
            }

            if (!string.IsNullOrWhiteSpace(primaryKey)) return primaryKey;

            var field = propertys[0];
            var cas = field.GetCustomAttributes(true);

            if (cas == null || cas.Length <= 0) return field.Name;

            colAttr = cas[0] as GFluentColumn;

            return string.IsNullOrWhiteSpace(colAttr.ColumnName) ? field.Name : colAttr.ColumnName;
        }

        #endregion
    }
}
